import { getProListReq, getRecommendOrSeckillListReq } from '@/service/pro'
import { Effect, ImmerReducer } from 'umi'
import { IPro } from './pro'
export interface IProState {
  proList: Array<IPro>
  recommendList: Array<IPro>
  seckillList: Array<IPro>
}
export interface IProModel {
  namespace: 'pro' // 命名空间写的和 正式定义时一致
  state: IProState,
  effects: {
    getProListEffect: Effect,
    getRecommendListEffect: Effect,
    getSeckillListEffect: Effect,
  },
  reducers: {
    // LOGIN: Reducer<ILoginState>
    // 启用 immer 之后
    UPDATE_PRO_LIST: ImmerReducer,
    UPDATE_RECOMMEND_LIST: ImmerReducer,
    UPDATE_SECKILL_LIST: ImmerReducer
  }
}
const proModel: IProModel = {
  namespace: 'pro',
  state: {
    proList: [],
    recommendList: [],
    seckillList: []
  },
  effects: {
    *getProListEffect ({ payload }, { call, put }) {
      const res = yield call(getProListReq, payload)
      yield put({
        type: 'UPDATE_PRO_LIST',
        payload: res.data
      })
    },
    *getRecommendListEffect ({ payload }, { call, put }) {
      const res = yield call(getRecommendOrSeckillListReq, payload)
      console.log('recommend', res)
      yield put({
        type: 'UPDATE_RECOMMEND_LIST',
        payload: res.data
      })
    },
    *getSeckillListEffect ({ payload }, { call, put }) {
      const res = yield call(getRecommendOrSeckillListReq, payload)
      yield put({
        type: 'UPDATE_SECKILL_LIST',
        payload: res.data
      })
    }
  },
  reducers: {
    UPDATE_PRO_LIST (state, action) {
      state.proList = action.payload
    },
    UPDATE_RECOMMEND_LIST (state, action) {
      state.recommendList = action.payload
    },
    UPDATE_SECKILL_LIST(state, action) {
      state.seckillList = action.payload
    },
  }
}

export default proModel
